<?php
namespace app\api\controller\audience;
use app\api\controller\common\ApiBase;
use Think\Db; 
use app\api\controller\common\CommonPlus;
use PHPExcel_IOFactory;
use PHPExcel;
use app\api\model\Admin as AdminModel;
use think\Controller; // dengh quxiao
// Controller  ApiBase （dengl）
class Guest extends ApiBase
{
    public function _initialize()
    {
        parent::_initialize();
        $this->admin = session('admin');
    }

    public function getTabName($exhibition_id){
        $result=db('exhibition')->where(['id'=>$exhibition_id])->find();
        $tabName='';
        if($result){
            $tabName=$result['create_person_table'];
        }else{
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        return $tabName;
    }

    /**
     * 预登记观众列表  0527
     * @return \think\response\Json|void
     *  // zh_20200528fmr
     */

    public function index(){
        $page = $this->request->param('page');
        $page = max(1,intval($page));
        $pagesize =$this->request->param('pageSize')==""?20:$this->request->param('pageSize');
        $exhibition_id =$this->request->param('exhibition_id');
        $activity_name =$this->request->param('activity_name');
        $activity_link =$this->request->param('activity_link');
        $keyword =input('keyword');
        if(empty($exhibition_id)){
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        $param=" and g.exhibition_id=$exhibition_id and g.is_delete=0";
        if (!empty($activity_name)) {
            $param.=" and g.activity_name=$activity_name";
        }
        if (!empty($activity_link)) {
            $param.=" and g.activity_link=$activity_link"; 
        }
        if (!empty($keyword)) {
            $param.=" and g.name like '%$keyword%'";
        }
        $sql2="select g.*,u.title as activity_names,q.title_huanjie as activity_links";
        $sql2.=" from crm_guest as g";
        $sql2.=" left join crm_zh_active as u on u.id=g.activity_name";
        $sql2.=" left join crm_zh_active_huanjie as q on q.id=g.activity_link";
        $sql2.=" where 1=1 $param";
        $sql2.=" limit ".($page-1)*$pagesize.",$pagesize";
        $list=Db::query($sql2);
        $sql21=" select count(g.id) as count1 ";
        $sql21.=" from crm_guest as g where 1=1 $param ";
        $total1=Db::query($sql21);
        $total=$total1[0]['count1']; //st
        return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'count'=>$total]]);
    }

    //编辑
    public function edit(){
        $id =$this->request->param('id');
        $data['name'] =$this->request->param('name');
        $data['exhibition_id'] =$this->request->param('exhibition_id');
        $data['activity_name'] =$this->request->param('activity_name');
        $data['activity_link'] =$this->request->param('activity_link');
        $data['is_hotel'] =$this->request->param('is_hotel');
        $data['arrive_time'] =$this->request->param('arrive_time');
        if(!empty($data['arrive_time'])){
            $data['arrive_time']=strtotime( $data['arrive_time']);
        }
        $data['update_time'] =time();
        $res=db("guest")->where(['id'=>$id])->find();
        if(!$res){
            return json(['code'=>0,'msg'=>'嘉宾数据有误','data'=>'']);
        }
        if(empty($data['exhibition_id'])){
            return json(['code'=>0,'msg'=>'展会数据有误','data'=>'']);
        }
        if(empty($data['arrive_time'])){
            return json(['code'=>0,'msg'=>'到达时间数据有误','data'=>'']);
        }
        $result=db("guest")->where(['id'=>$id])->update($data);
        if(!$result){
            return json(['code'=>0,'msg'=>'编辑错误','data'=>'']);
        }
        return json(['code'=>1,'msg'=>'编辑成功','data'=>'']);
    }


    //新增
    public function add(){
        $data['name'] =$this->request->param('name');
        $data['exhibition_id'] =$this->request->param('exhibition_id');
        $data['activity_name'] =$this->request->param('activity_name');
        $data['activity_link'] =$this->request->param('activity_link');
        $data['is_hotel'] =$this->request->param('is_hotel');
        $data['arrive_time'] =$this->request->param('arrive_time');
        if(!empty($data['arrive_time'])){
            $data['arrive_time']=strtotime( $data['arrive_time']);
        }
        $data['update_time'] =time();
        $data['create_time'] =time();
        if(empty($data['exhibition_id'])){
            return json(['code'=>0,'msg'=>'展会数据有误','data'=>'']);
        }
        if(empty($data['arrive_time'])){
            return json(['code'=>0,'msg'=>'到达时间数据有误','data'=>'']);
        }
        $result=db("guest")->insert($data);
        if(!$result){
            return json(['code'=>0,'msg'=>'保存错误','data'=>'']);
        }
        return json(['code'=>1,'msg'=>'保存成功','data'=>'']);
    }


    //删除
    public function del(){
        $ids=$this->request->param('ids');
        if(empty($ids)){
            return json(['code'=>0,'msg'=>'观众数据有误','data'=>'']);
        }
        $person_id = explode(',',$ids);
        foreach ($person_id as &$v){
            $res=db("guest")->where(['id'=>$v])->find();
            if($res && $res['is_delete']!=1){
                $res=db("guest")->where(['id'=>$v])->update(['is_delete'=>1]);
            }
        }
        unset($v);
        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
    }


    //活动列表
    public function activity(){
        $exhibition_id =$this->request->param('exhibition_id');
        if(empty($exhibition_id)){
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
         }
        $param=" and is_del=0 and exhibition_id=$exhibition_id ";
        $param.=" order by id desc";
        $sql2="select title,id from crm_zh_active ";
        $sql2.=" where 1=1 $param";
        $list=Db::query($sql2);
        return json(['code'=>1,'msg'=>'success','data'=>$list]);
    }
    //环节列表
    public function link(){
        $id = $this->request->param('id');//活动id
        if(empty($id)){
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
         $list=db('zh_active_huanjie')->where(['id_hd'=> $id,'is_del'=>0])->select();
        return json(['code'=>1,'msg'=>'success','data'=>$list]);
    }

    // 导出  stj ce
    public function export()
    {
        $exhibition_id =$this->request->param('exhibition_id');
        $activity_name =$this->request->param('activity_name');
        $activity_link =$this->request->param('activity_link');
        $ids=$this->request->param('ids');
        $keyword =input('keyword');
        if(empty($exhibition_id)){
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }

        $param=" and g.is_delete=0 and g.exhibition_id=$exhibition_id ";
        if(empty($ids)){
            if (!empty($activity_name)) {
                $param.=" and g.activity_name=$activity_name";
            }
            if (!empty($activity_link)) {
                $param.=" and g.activity_link=$activity_link"; 
            }
            if (!empty($keyword)) {
                $param.=" and g.name like '%$keyword%'";
            }
        }else {
            $param.=" and id in ($ids)";
        }

        $sql2="select g.*,u.title as activity_names,q.title_huanjie as activity_links";
        $sql2.=" from crm_guest as g";
        $sql2.=" left join crm_zh_active as u on u.id=g.activity_name";
        $sql2.=" left join crm_zh_active_huanjie as q on q.id=g.activity_link";
        $sql2.=" where 1=1 $param";
        $list=Db::query($sql2);
        foreach ($list as $key => $value) {
            $list[$key]['arrive_time_date'] = date("Y-m-d H:i:s",$value['arrive_time']) ;
            $list[$key]['is_hotel_str'] = $value['is_hotel']==0?'否':'是';
        }
        $columns = array(
            array('title' => '嘉宾名称', 'field' => 'name', 'width' => 12),
            array('title' => '活动名称', 'field' => 'activity_names', 'width' => 12),
            array('title' => '环节名称', 'field' => 'activity_links', 'width' => 12),
            array('title' => '是否预定酒店', 'field' => 'is_hotel_str', 'width' => 12),
            array('title' => '到达时间', 'field' => 'arrive_time_date', 'width' => 12),

        );
        $title = "嘉宾明细";
        $filename = $title . date('Y-m-d-H-i', time()) . ".xls";
        return json(['code'=>1,'msg'=>'操作成功','data'=>['columns'=>$columns,'filename'=>$filename,'list'=>$list]]);
    }


    public function saveExcel(){
        $filename=$this->request->param('filename');
        $exhibition_id =$this->request->param('exhibition_id');
        if(empty($filename)){
            return json(['code'=>0,'msg'=>'excel缺失，请重新上传','data'=>'']);
        }
        if(empty($exhibition_id)){
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        $tabName=$this->getTabName($exhibition_id);
        $filename = ROOT_PATH.'public/uploads/excel/'.$filename;
        $reader = PHPExcel_IOFactory::createReaderForFile($filename);
        $excel = $reader->load($filename, $encode = 'utf-8');
        $excel_array=$excel->getsheet(0)->toArray();  //转换为数组格式
        array_shift($excel_array);  //删除第一个数组(标题);
        if (!is_array($excel_array) || empty($excel_array)) {
            return false;
        } else {
            //循环读取数据
            foreach ($excel_array as $key => $value) {
                $list[$key]['name'] = (string) $value[0];//嘉宾名称
                $list[$key]['activity_name'] = (string) $value[1]; //活动名称
                $list[$key]['activity_link'] = (string) $value[2]; //环节名称
                $list[$key]['is_hotel'] = (string) $value[3]; // 是否预定酒店
                $list[$key]['arrive_time'] =strtotime($value[4]); // 到达时间
            }
        }
        foreach ($list as &$v){
            $guest=db('guest')->where(['name'=>$v['name']])->find();
            if($guest){
                $guest=db('guest')->where(['id'=>$guest['id']])->update($v);
            }else{
                $guest=db('guest')->insert($v);
            }
            unset($v);
        }
        
        return json(['code' => 1, 'msg' => '操作成功']);
    }

}